package server.imp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import listener.Listener;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import server.Server;
import service.impl.WebServiceImpl;

public class WebServerImpl extends Thread implements Server{
	private final static Logger logger = LogManager.getLogger(WebServerImpl.class);
	
	private ServerSocket server;
	private Listener listener;
	
	public void server(){
		try {
			server = new ServerSocket(9000);
			logger.info("web server start");
			while (true) {
				Socket sk=server.accept();
				WebServiceImpl service = new WebServiceImpl(sk);
				service.setListener(listener);
				service.service();
			}
		} catch (IOException e) {
			logger.error(e);
		}
		catch (Exception e) {
			logger.error(e);
		}
	}
	
	@Override
	public void run(){
		server();
	}
	
	public static void main(String[] args) {
		
	}

	@Override
	public void shutdown() {
		try {
			server.close();
		} catch (IOException e) {
			logger.warn(e);
		}
	}

	public void listen(Listener listener) {
		this.listener = listener;
		
	}

}
